/* 
 * File: SingleTouchHandler
 * Created: 13-JUN-2011
 * Author: Colin Gall-McDaid
 * 
 * Description: Touch handler for single touch Android devices. These are all
 * devices running Android versions below 2.0.
 */
package com.gallsoft.games.framework.impl;

import android.view.MotionEvent;
import android.view.View;

import com.gallsoft.games.framework.Input.TouchEvent;


public class SingleTouchHandler extends BaseHandler {
    
    private int touchX;
    private int touchY;
    private boolean isTouched;

    /**
     * Constructor
     * 
     * @param view the Android view
     * @param scaleX ???
     * @param scaleY ???
     */
    public SingleTouchHandler(View view, float scaleX, float scaleY) {
        super(view, scaleX, scaleY);
    }
    
    /**
     * Implementation of the OnTouchListener.onTouch method
     * 
     * @param v the Android view
     * @param event the touch event that has occured in the view
     */
    public boolean onTouch(View v, MotionEvent event) {
        synchronized (this) {
            TouchEvent touchEvent = touchEventPool.newObject();
            
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    touchEvent.type = TouchEvent.TOUCH_DOWN;
                    isTouched = true;
                    break;
                    
                case MotionEvent.ACTION_MOVE:
                    touchEvent.type = TouchEvent.TOUCH_DRAGGED;
                    isTouched = true;
                    break;
                    
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    touchEvent.type = TouchEvent.TOUCH_UP;
                    isTouched = false;
                    break;
            }
            
            touchEvent.x = touchX = (int)(event.getX() * scaleX);
            touchEvent.y = touchY = (int)(event.getY() * scaleY);
            touchEventsBuffer.add(touchEvent);
            
            return true;
        }
    }

    /**
     * Check if screen is touched
     * 
     * @param pointer pointer id
     * @return true or false
     */
    public boolean isTouchDown(int pointer) {
        synchronized (this) {
            if (pointer == 0)
                return isTouched;
            else
                return false;
        }        
    }

    /**
     * Get x coordinate of touch position
     * 
     * @param pointer pointer id (only 0 is valid for single touch)
     * @return x coordinate of touch position
     */
    public int getTouchX(int pointer) {
        synchronized (this) {
            return touchX;
        }
    }

    /**
     * Get y coordinate of touch position
     * 
     * @param pointer pointer id (only 0 is valid for single touch)
     * @return y coordinate of touch position
     */
    public int getTouchY(int pointer) {
        synchronized (this) {
            return touchY;
        }
    }

}
